﻿using System;
using System.Windows;
using appDataContracts;

namespace appAdmin
{
    /// <summary>
    /// Logique d'interaction pour AppWindow.xaml
    /// </summary>
    public partial class showSuppliers : Window
    {
        private appAdmin.myAdminService.AdminClient adminService = null;        
        private SerializedSupplier supplier = null;
        private AppWindow appWindow = null;
        private bool updateSupplier = true;

        public showSuppliers(appAdmin.myAdminService.AdminClient adminService,
            AppWindow appWindow, SerializedSupplier supplier)
        {
            this.adminService = adminService;
            this.appWindow = appWindow;
            this.supplier = supplier;
            InitializeComponent();

            if (supplier == null)
            {
                updateSupplier = false;
                this.supplier = new SerializedSupplier();
                dpDateLimite.SelectedDate = DateTime.Now.AddYears(1);
            }

            if (updateSupplier)
            {                
                tbNom.Text = supplier.SUPPLIERNAME;
                tbAdresse.Text = supplier.SUPPLIERADRESS;
                tbZIP.Text = supplier.SUPPLIERZIP;
                tbVille.Text = supplier.SUPPLIERCITY;
                tbPays.Text = supplier.SUPPLIERCOUNTRY;
                tbMail.Text = supplier.SUPPLIERMAIL;
                tbTelephone.Text = supplier.SUPPLIERPHONE;
                pbMotDePasse.Password = supplier.SUPPLIERPASSWORD;
                dpDateLimite.SelectedDate = supplier.SUPPLIERDATELIMIT;
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btValidate_Click(object sender, RoutedEventArgs e)
        {            
            supplier.SUPPLIERNAME = tbNom.Text;
            supplier.SUPPLIERADRESS = tbAdresse.Text;
            supplier.SUPPLIERZIP = tbZIP.Text;
            supplier.SUPPLIERCITY = tbVille.Text;
            supplier.SUPPLIERCOUNTRY = tbPays.Text;
            supplier.SUPPLIERMAIL = tbMail.Text;
            supplier.SUPPLIERPHONE =  tbTelephone.Text;
            supplier.SUPPLIERPASSWORD = pbMotDePasse.Password;
            supplier.SUPPLIERDATELIMIT = dpDateLimite.SelectedDate.Value;

            if (updateSupplier)
            {
                if (adminService.updateSupplier(supplier)) this.appWindow.update();
            }
            else
            {
                if (adminService.addSupplier(supplier)) this.appWindow.update();
            }

            this.Close();
        }
    }
}
